package com.charger.transactionflow.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.charger.PromoteOperations.domain.Client;
import com.charger.PromoteOperations.domain.ClientNewclient;
import com.charger.PromoteOperations.domain.Newclient;
import com.charger.PromoteOperations.service.ClientNewclientService;
import com.charger.PromoteOperations.service.IClientService;
import com.charger.PromoteOperations.service.NewclientService;
import com.charger.PromoteOperations.util.IntegralCalculator;
import com.charger.charger.domain.Charger;
import com.charger.chargingStation.domain.Chargingstation;
import com.charger.chargingStation.service.IChargingstationService;
import com.charger.trade.domain.Chargingrecords;
import com.charger.trade.service.IChargingrecordsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.transactionflow.domain.Transactionflow;
import com.charger.transactionflow.service.ITransactionflowService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 账单流水Controller
 * 
 * @author ruoyi
 * @date 2024-11-23
 */
@RestController
@RequestMapping("/transactionflow/transactionflow")
public class TransactionflowController extends BaseController
{
    @Autowired
    private ITransactionflowService transactionflowService;

    @Autowired
    private IChargingrecordsService chargingrecordsService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private IChargingstationService chargingstationService;
    @Autowired
    private ClientNewclientService clientNewclientService;
@Autowired
private NewclientService newclientService;
    /**
     * 查询账单流水列表
     */
    @PreAuthorize("@ss.hasPermi('transactionflow:transactionflow:list')")
    @GetMapping("/list")
    public TableDataInfo list(Transactionflow transactionflow)
    {
        startPage();
        List<Transactionflow> list = transactionflowService.selectTransactionflowList(transactionflow);
        for (Transactionflow aa:list){
            Chargingrecords chargingrecords = chargingrecordsService.selectChargingrecordsByChargingrecordsId(aa.getChargingrecordsId());
            aa.setChargingrecords(chargingrecords);
        }

        for (Transactionflow aa:list){
            Client client = clientService.selectClientById(aa.getClientId());
            aa.setClient(client);
        }
        for (Transactionflow aa:list){
            Chargingstation chargingstation = chargingstationService.selectChargingstationByChargingstationId(aa.getChargingstationId());
            aa.setChargingstation(chargingstation);
        }
        return getDataTable(list);
    }

    /**
     * 导出账单流水列表
     */
    @PreAuthorize("@ss.hasPermi('transactionflow:transactionflow:export')")
    @Log(title = "账单流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Transactionflow transactionflow)
    {
        List<Transactionflow> list = transactionflowService.selectTransactionflowList(transactionflow);
        ExcelUtil<Transactionflow> util = new ExcelUtil<Transactionflow>(Transactionflow.class);
        util.exportExcel(response, list, "账单流水数据");
    }

    /**
     * 获取账单流水详细信息
     */
    @PreAuthorize("@ss.hasPermi('transactionflow:transactionflow:query')")
    @GetMapping(value = "/{transactionflowId}")
    public AjaxResult getInfo(@PathVariable("transactionflowId") Long transactionflowId)
    {
        return success(transactionflowService.selectTransactionflowByTransactionflowId(transactionflowId));
    }

    /**
     * 新增账单流水
     */
    @PreAuthorize("@ss.hasPermi('transactionflow:transactionflow:add')")
    @Log(title = "账单流水", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Transactionflow transactionflow) {
        if (transactionflow.getTransactionType().equals("已退款")){
            Long clientId = transactionflow.getClientId();
            Long chargingrecordsId = transactionflow.getChargingrecordsId();
            Chargingrecords chargingrecords = chargingrecordsService.selectChargingrecordsByChargingrecordsId(chargingrecordsId);
            String expenses = chargingrecords.getExpenses();

            double xs=new Double(expenses);
            Long aLong = IntegralCalculator.moneyIntegral((long) xs);

            Client client = clientService.selectClientById(clientId);
            Long integral = client.getIntegral();
            client.setIntegral(integral-aLong);
            clientService.updateClient(client);
            String name = client.getName();
            QueryWrapper<Newclient> wrapper = new QueryWrapper<>();
            wrapper.eq("name", name);
            Newclient newclient = newclientService.getOne(wrapper);
            if (newclient == null){
                return toAjax(transactionflowService.insertTransactionflow(transactionflow));
            }
            Long id1 = newclient.getId();
//            Long id = client.getId();

            QueryWrapper<ClientNewclient> clientQueryWrapper = new QueryWrapper<>();
            clientQueryWrapper.eq("newclient_id",id1);
            ClientNewclient one = clientNewclientService.getOne(clientQueryWrapper);
            if (one.getIsNewinivit().equals("1")){
                Client client1 = clientService.selectClientById(one.getClientId());
                Long integral1 = client1.getIntegral();
                client1.setIntegral(integral1-aLong);
                clientService.updateClient(client1);
            }

        }
//        判断是否为退款
//        通过充电记录外键查询充电记录信息
//        提取出消费金额
//        将消费金额通过积分计算器计算出积分
//        将积分减去退款积分，并将剩余积分更新到用户表中
//        判断该用户是否曾为新用户（通过用户姓名判断）
//        若曾为新用户，根据clientnewclient表查询出该用户的记录，提取出其好友id
//        在客户表中查询出该好友，将积分减去退款积分，将剩余积分更新到客户表中
        return toAjax(transactionflowService.insertTransactionflow(transactionflow));
    }

    /**
     * 修改账单流水
     */
    @PreAuthorize("@ss.hasPermi('transactionflow:transactionflow:edit')")
    @Log(title = "账单流水", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Transactionflow transactionflow)
    {
        return toAjax(transactionflowService.updateTransactionflow(transactionflow));
    }

    /**
     * 删除账单流水
     */
    @PreAuthorize("@ss.hasPermi('transactionflow:transactionflow:remove')")
    @Log(title = "账单流水", businessType = BusinessType.DELETE)
	@DeleteMapping("/{transactionflowIds}")
    public AjaxResult remove(@PathVariable Long[] transactionflowIds)
    {
        return toAjax(transactionflowService.deleteTransactionflowByTransactionflowIds(transactionflowIds));
    }
}
